package com.susu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.susu.pojo.Student;
import com.susu.service.impl.StudentServiceImpl;
import com.susu.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class StudentController {

    @Autowired
    private StudentServiceImpl studentService;

    //分页添加查询
    @PostMapping("/getStudents/{pageNum}")
    public ResultVo getStudents(@PathVariable Integer pageNum, String studentname) {
        Page<Student> page = new Page<>(pageNum, 3);

        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        if (studentname != null && !studentname.equals("")) {
            wrapper.like(Student::getStudentname, studentname);
        }
        List<Student> students = studentService.list(page, wrapper);
        
        page.setRecords(students);
        if (students != null) return ResultVo.success("2000", page);

        return ResultVo.error("5000");
    }

    //添加与修改
    @PostMapping("/addStudent")
    public ResultVo getStudents(@RequestBody Student student) {
        if (studentService.saveOrUpdate(student)) return ResultVo.success("2000", null);
        return ResultVo.error("5000");
    }

    //删除
    @GetMapping("/delete/{id}")
    public ResultVo deleteStudent(@PathVariable Integer id) {
        if (studentService.removeById(id)) return ResultVo.success("2000", null);
        return ResultVo.error("5000");
    }
}
